new Vue({
    el:'#app',
    data: {
        goodsnum : 1,
        goodsname : "",
        goodstag : "",
        goodspic : "",
        goodsprice : 0,
        allgoodsprice : 0,
        commentlist:[],
        gid:1,
        uid:1,
        sid:1,
        hascollect:false,
        hasadd:false,
        hastopay:false,
    },
    created(){


        let url = window.location.href;
        let info = url.split("?")[1].split("&");
        let gid = info[0].split("=")[1];
        let uid = info[1].split("=")[1];
        let sid = info[2].split("=")[1];
        this.gid = gid;
        this.uid = uid;
        this.sid = sid;
        console.log(uid);
        this.uid=localStorage.getItem("user_id");
        console.log(uid);
        axios({
            method: 'get',
            url: 'http://47.94.138.204:7790/goods/goodsdetails',
            params:{
                sid :gid,
            },
        }).then((res) => {
            let data = res.data;
            this.goodsname = data.name;
            this.goodstag = data.property;
            this.goodsprice = data.price;
            this.allgoodsprice = data.price;
            this.goodspic = data.avatarUrl;
            document.getElementById("goodslogo").setAttribute("src",this.goodspic);
        })

        axios({
            method: 'get',
            url: 'http://47.94.138.204:7790/example/findcomment',
            params:{
                gid :gid,
            },
        }).then((res) => {
            let data = res.data;
            this.commentlist = data;
            console.log(this.commentlist);
        })

        axios({
            method: 'get',
            url: 'http://47.94.138.204:7790/example/iscollected',
            params:{
                gid :gid,
                uid: uid,
            },
        }).then((res) => {
            let data = res.data;
            if (data == "1"){
                this.hascollect = true;
            }

            console.log(data,"hahahhahhahah")
        })


        axios({
            method: 'get',
            url: 'http://47.94.138.204:7790/example/findshoppingcar',
            params:{
                uid : 6,
            },
        }).then((res) => {
            let data = res.data;
            console.log(data);
            for(let i = 0; i < data.length; i ++ ){
                console.log(data[i].sid, this.sid)
                if(data[i].sid == this.gid){
                    this.hasadd = true;
                }
            }
        })


    },
    watch:{
        'goodsnum'(newName,oldName){
            this.allgoodsprice = this.goodsprice * this.goodsnum;
        }
    },

    methods:{
        collect:function () {
            if (this.hascollect)
            {
                alert("您已经收藏了");
            }else{
                axios({
                    method: 'get',
                    url: 'http://47.94.138.204:7790/example/addcollect',
                    params:{
                        gid : this.gid,
                        uid : this.uid,
                        sid : this.sid,
                    },
                }).then((res) => {
                    let data = res.data;
                    this.hascollect = true;
                })

            }
        },
        addCart:function () {

            if(!this.hasadd){
                axios({
                    method: 'get',
                    url: 'http://47.94.138.204:7790/example/addshoppingcar',
                    params:{
                        gid : this.gid,
                        uid : this.uid,
                        num : this.goodsnum,
                        time: "20200924",
                    },
                }).then((res) => {
                    alert("加入成功");
                    this.hasadd = true;
                })

            }else{
                alert("已经加入购物车了");
            }
        },
        toCart:function () {
            window.location.href = "shopping-cart.html?uid="+this.uid;
        },
        gotohadpay:function () {
            window.location.href = "payment.html?uid=" + this.uid;
        },
    },


})